package com.wy.component.dataConvert;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Image {
    public static void main(String[] args) {
        // 测试从Base64编码转换为图片文件
        String strImg = "iVBORw0KGgoAAAANSUhEUgAAAEIAAABCCAYAAADjVADoAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2lpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8 IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDoxM2M1YTQ5ZC0yZjA4LTM1NDYtYmQzYS04ZmIwZTE0ODljNDMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6Njg5RTZDRDRENUVEMTFFNUE5MkNFMTU1NkM5N0ZCRTMiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5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/5wfcTWOBEhYNPApkic5XIRf1YYydQYF6StnR0Qhb6tLrhACurSmLEmp2m0zEiEsMruTnD 0OzSXh8ADnB4L/N7tsQRtAZQ4mIRyL6BjeBlLAOqShuSKCeMQvI7sRTivYccY30shWj1kBCNsRTCL LUEoSysJKuUDZMdC7JUVxfy VSoa87Uvshoa9rJhk t47/5gj9Bxxa5S7miNbPZVqdyuA8odQQMufxdmmwHMLpETEQFDFTDeVkXjzJ5/1WG/YrMnw g9sEX2CRoZzPZeMvXTmGYySrqaF8hBJCCaGEUEIoIZQQSgglhBJCCRHv2afZKBs8A1qE/uBo4Cf986bsM2B13EZ2FzSIzrWPW C1HRlmdwjRYEixLwt9uZ2shP8Gyhpv04Nmz7jd2GYsG/f3jBBWJ 4T8sVfrQsXHDMRYu0jtBAXrkkE0izKwY7lGWfpk1yQWRQhqfeFOJYrpwY5threbpSctJUYVj2tBRGAtosN39fuJiHI68 w6MFQPRzOaAjYSUbFESqgUkIoIZQQUdogJYRu2UoI/YH1VU7HESQm/WTX3wVTgR5KnST0twVS7BKCXhxJC/H54Zw2T/TISGmLZGpUd HA5R4SQfDaSNhC0Mtl5tePzHG9l0SgXGV/JELQE3T00OlPLrcYEp1wRo0bjF6YKwl2vqGc5XWhv9JMT8DTSpN5uayMnRK9z53ksovvYD9HbwoeEfoKmPVagfq3CSrEDj4i/lf7K8AAVFrbWODqXSQAAAAASUVORK5CYII=";
        GenerateImage(strImg, "D:\\wangyc.jpg");
        
        // 测试从图片文件转换为Base64编码
//        System.out.println(GetImageStr("d:\\wangyc.jpg"));
    }

    public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        if(imgStr.contains("base64,"))
        	imgStr = imgStr.substring(imgStr.indexOf("base64,")+7);
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}